'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 进入战斗
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var async = require('async');
var Code = require('../../../../consts/code');
var atkData = require('../../../../libs/war/atkData');
var battleData = require('../../../../libs/war/battleData');
var onlineService = require('../../../../services/onlineService');
var taskService = require('../../../../services/taskService');
var battleService = require('../../../../services/battleService');
var PlayerManager = require('../../../../libs/playerManager');
var dataApi = require('../../../../libs/dataApi');
var rivalService = require('../../../../services/rivalService');
var pvpGuideData = require('../../../../libs/war/pvpGuideData');
var buildTypeUtil = require('../../../../util/buildTypeUtil');
var buildType = require('../../../../consts/buildType');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var pvp_guide_id = msg.pvp_guide_id;
    if (!pvp_guide_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var pvp_guide_hero_conf = dataApi.pvp_guide_hero.findById(pvp_guide_id);
    var pvp_guide_war_conf = dataApi.pvp_guide_war.findById(pvp_guide_id);

    var rival_arrow_type_list = [];
    var rival_barrack_list = [];
    var rival_road_list = [];
    var rival_arrow_list = [];
    _.each(pvp_guide_war_conf, function (guide_conf) {
        var build_type = buildTypeUtil.getBuildType(guide_conf.building_id);
        if (guide_conf.building_id == buildType.ROAD) {
            rival_road_list.push(guide_conf.coordinate);
        } else if (buildTypeUtil.isTower(build_type)) {
            if (_.indexOf(rival_arrow_type_list, build_type) == -1) {
                rival_arrow_type_list.push(build_type);
            }
            rival_arrow_list.push(guide_conf.coordinate);
        } else if (build_type == buildType.BARRACK) {
            rival_barrack_list.push(guide_conf.coordinate);
        }
    });

    return next(null, {
        code: Code.OK,
        result: {
            rival: {
                role_id: 10000,
                job_type: 1,
                gold: 500,
                cup: 1,
                food: 500,
                name: pvp_guide_hero_conf.name,
                noble: 0,
                lv: 1,
                road_list: rival_road_list,
                arrow_list: rival_arrow_list,
                arrow_type_list: rival_arrow_type_list,
                soldier_list: pvpGuideData.getSoldierTypeList(pvp_guide_id),
                barrack_list: [],
                main_power: 0
            }
        }
    });
};
